package com.ridaizu.dao.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.ridaizu.dao.DaoAdapter;
import com.ridaizu.dao.db.DBAccess;
import com.ridaizu.model.BrandSeriesManager;
import com.ridaizu.model.Model;
import com.ridaizu.model.search.SearchModel;

@ManagedBean(name = "brandSeriesManagerDao")
@ApplicationScoped
public class BrandSeriesManagerDao extends DaoAdapter {

	private static final long serialVersionUID = 1L;

	public int selectCount(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from m_brand_series_manager \n");
		sql.append(searchModel.getSelectCountSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		if (rs == null) {
			return -1;
		}

		rs.next();

		return rs.getInt(1);
	}

	public List<BrandSeriesManager> selectBrandSeriesManagerList(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT \n");
		sql.append("bsm.id as id, \n");
		sql.append("bsm.brand_id as brand_id, \n");
		sql.append("b.name as brand_name, \n");
		sql.append("bsm.brand_series_id as brand_series_id, \n");
		sql.append("bs.name as brand_series_name, \n");
		sql.append("bsm.rgt_opt as rgt_opt, \n");
		sql.append("bsm.rgt_date as rgt_date, \n");
		sql.append("bsm.upd_opt as upd_opt, \n");
		sql.append("bsm.upd_date as upd_date \n");
		sql.append("FROM m_brand b, m_brand_series_manager bsm, m_brand_series bs \n");
		sql.append(searchModel.getSelectSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		List<BrandSeriesManager> modelList = new ArrayList<BrandSeriesManager> ();
		while (rs.next()) {
			modelList.add((BrandSeriesManager)getModel(rs));
		}

		rs.close();

		return modelList;
	}

	@Override
	public Model getModel(ResultSet rs) throws SQLException {

		checkNotNull(rs);
		BrandSeriesManager brandSeriesManager = new BrandSeriesManager();

		brandSeriesManager.setId(rs.getInt("id"));
		brandSeriesManager.setBrandId(rs.getInt("brand_id"));
		brandSeriesManager.setBrandName(rs.getString("brand_name"));
		brandSeriesManager.setBrandSeriesId(rs.getInt("brand_series_id"));
		brandSeriesManager.setBrandSeriesName(rs.getString("brand_series_name"));
		brandSeriesManager.setRgtOpt(rs.getString("rgt_opt"));
		brandSeriesManager.setRgtDate(rs.getTimestamp("rgt_date"));
		brandSeriesManager.setUpdOpt(rs.getString("upd_opt"));
		brandSeriesManager.setUpdDate(rs.getTimestamp("upd_date"));

		return brandSeriesManager;
	}

	@Override
	public int insert(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO m_brand_series_manager \n");
		sql.append("( \n");
		sql.append("brand_id, brand_series_id, rgt_opt, \n");
		sql.append("rgt_date, upd_opt, upd_date \n");
		sql.append(") \n");
		sql.append("VALUES \n");
		sql.append("(?, ?, ?, ?, ?, ?); \n");

		BrandSeriesManager brandSeriesManager = (BrandSeriesManager) model;

		Object[] params = new Object[] {
				brandSeriesManager.getBrandId(), brandSeriesManager.getBrandSeriesId(), brandSeriesManager.getRgtOpt(),
				brandSeriesManager.getRgtDate(), brandSeriesManager.getUpdOpt(), brandSeriesManager.getUpdDate()
		};

		return DBAccess.update(sql.toString(), params);
	}

	@Override
	public int update(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE m_brand_series_manager \n");
		sql.append("SET \n");
		sql.append("brand_id = ?, \n");
		sql.append("brand_series_id = ?, \n");
		sql.append("rgt_opt = ?, \n");
		sql.append("rgt_date = ?, \n");
		sql.append("upd_opt = ?, \n");
		sql.append("upd_date = ? \n");
		sql.append("WHERE id = ? \n");

		BrandSeriesManager brandSeriesManager = (BrandSeriesManager) model;

		Object[] params = new Object[] {
				brandSeriesManager.getBrandId(), brandSeriesManager.getBrandSeriesId(), brandSeriesManager.getRgtOpt(),
				brandSeriesManager.getRgtDate(), brandSeriesManager.getUpdOpt(), brandSeriesManager.getUpdDate(),
				brandSeriesManager.getId()
		};

		return DBAccess.update(sql.toString(), params);
	}

	public int delete(int id) {
		String sql = "DELETE FROM m_brand_series_manager WHERE id=?;";

		return DBAccess.update(sql.toString(), id);
	}

}
